package com.clf.common.util.page;

import com.clf.common.util.util.StringUtil;
import lombok.ToString;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Description：分页的基础类
 *
 * @author <a href="mailto:yrh@qtz360.com">yrh</a>
 * @Date Create on 2015年9月16日
 * @since version1.0 Copyright 2018 QTZ All Rights Reserved.
 */
@ToString
public class PageQuery implements Serializable {

    private static final long serialVersionUID = 2111713791626992056L;

    /**
     * must lower case!
     */
    private static final String[] sqlKeys = {";", "select", "delete", "update", "create", "drop", "insert", "alter",
            "truncate", "declare", "\\", "/", "=", "#", "$", "&", "(", ")", "exec ", " or "};

    private static final String[] filterkeys = {"_create"};

    public static final int CLIENT_PAGING = 1;
    public static final int SERVER_PAGING = 2;// 后台数据分页模式，不通过前台分页控件控制分页数

    // 是否分页 设为false后查询所有的记录
    private boolean isPaging = false;

    /**
     * @return the maxpage
     */
    public int getMaxpage() {
        return maxpage;
    }

    public void setMaxpage(int maxPage) {

    }

    /**
     * 默认分页记录显示数
     */
    public static final int DEFAULT_PAGE_SIZE = 20;

    /**
     * 最大分页记录显示数
     */
    public static final int BIGEST_PAGE_SIZE = 50;

    private int pageSize = DEFAULT_PAGE_SIZE; // 每页记录数. webPage15,数据型应用10~25

    private int totalCount = 0; // 总记录数

    private int pageNo = 1; // 当前页码

    private int pageCount = 1; //总页数

    private int startRow = 0;//开始行数

    private int endRow = startRow + pageSize;//结束行数

    private int doPageType = CLIENT_PAGING;

    private String countAlias = ""; // 汇总字段的表或视图的别名.

    private String orderByClause; // order by

    private final int maxpage = 10;

    /**
     * 非数据库字段-标记
     */
    private String jflag;

    private int startPage = 1;

    private int endPage;

    public int getStartPage() {

        if (pageCount <= maxpage) {

            startPage = 1;
            return startPage;
        }
        if (pageNo + 5 <= maxpage) {
            startPage = 1;
            return startPage;
        }

        if (pageNo + 5 >= pageCount) {
            startPage = pageCount - maxpage + 1;
            return startPage;
        }

        startPage = pageNo + 5 - maxpage + 1;

        return startPage;
    }

    public void setStartPage(int startPage) {
    }

    public int getEndPage() {
        if (pageCount <= maxpage) {

            endPage = pageCount;
            return endPage;
        }
        if (pageNo + 5 <= maxpage) {
            endPage = maxpage;
            return endPage;
        }

        if (pageNo + 5 >= pageCount) {
            endPage = pageCount;
            return endPage;
        }

        endPage = pageNo + 5;

        return endPage;
    }

    public void setEndPage(int endPage) {

    }

    public String getJflag() {
        return jflag;
    }

    public void setJflag(String jflag) {
        this.jflag = jflag;
    }

    public PageQuery() {
    }

    /**
     * 设置分页各参数
     *
     * @param totalCount 总记录数
     */
    @Deprecated
    public void doPage(int totalCount) {
        if (totalCount < 0) {
            totalCount = 0;
        }
        this.totalCount = totalCount;

        if (pageSize <= 0)
            this.pageSize = DEFAULT_PAGE_SIZE;
        int mod = -1;
        if (totalCount != -1) {
            if (totalCount > pageSize) {
                mod = totalCount % pageSize;
                if (mod != 0) {
                    pageCount = (totalCount / pageSize) + 1;
                } else {
                    pageCount = (totalCount / pageSize);
                }
            } else {
                pageCount = 1;
            }
        }

        if (pageNo <= 0)
            this.pageNo = 1;
        if (this.doPageType != SERVER_PAGING) {
            if (pageNo > pageCount)
                this.pageNo = pageCount;
        }
        this.startRow = (this.pageNo - 1) * this.pageSize; // mysql: 0; Oracle:
        // 1
        this.endRow = this.pageNo * this.pageSize;
    }

    @Deprecated
    public void doPage(int totalCount, int pageNo, int pageSize) {
        this.pageNo = pageNo;
        this.pageSize = pageSize;
        this.doPage(totalCount);
    }

    /**
     * 是否为第一页
     *
     * @return
     */
    public boolean isFirstPage() {
        return this.getPageNo() == 1;
    }

    @Deprecated
    public void setFirstPage(boolean isFirstPage) {
    }

    /**
     * 是否是最有一页
     *
     * @return
     */
    public boolean isLastPage() {
        return this.getPageCount() == this.getPageNo();
    }

    @Deprecated
    public void setLastPage(boolean isLastPage) {
    }

    /**
     * currentPage The currentPage to set.
     */
    @Deprecated
    public void setCurrentPage(Integer cPage) {
        this.pageNo = ((cPage == null) || (cPage.intValue() <= 0)) ? 1 : cPage;
    }

    /**
     * 得到当前页
     *
     * @return
     */
    public int getPageNo() {
        return pageNo;
    }

    /**
     * 设置当前页
     *
     * @param pageNo
     */
    public void setPageNo(int pageNo) {
        if (pageNo < 1) {
            pageNo = 1;
        }
        this.pageNo = pageNo;
        reflashRange();
    }

    /**
     * 得到每页条数
     *
     * @return
     */
    public int getPageSize() {
        return pageSize;
    }

    /**
     * 设置每页条数
     *
     * @param iPageSize
     */
    public void setPageSize(int iPageSize) {
        if (iPageSize >= 1) {
            this.pageSize = iPageSize;
            reflashRange();
        }
    }


    protected void reflashRange() {
        startRow = (pageNo - 1) * pageSize;
        endRow = startRow + pageSize;
    }

    /**
     * 客户端程序设置总数 一般不推荐使用 该值自己算
     *
     */

    public int getTotalCount() {
        return this.totalCount == -1 ? 0 : totalCount;
    }

    /**
     * 设置总条数
     *
     * @param totalCount
     */
    public void setTotalCount(int totalCount) {
        if (totalCount < 0) {
            totalCount = 0;
        }
        this.totalCount = totalCount;
        reflashPageCount();

    }

    private void reflashPageCount() {
        pageCount = totalCount % pageSize == 0 ? (totalCount / pageSize) : (totalCount / pageSize) + 1;

    }

    /**
     * 得到总页数，如果未设置总记录数， 值可能是1
     *
     * @return
     */
    public int getPageCount() {
        return pageCount;
    }

    /**
     * 这个不能被客户端程序调用
     * 设置总页数
     *
     * @param pageCount
     */
    @Deprecated
    public void setPageCount(int pageCount) {
        if (pageCount < 1) {
            pageCount = 1;
        }
        this.pageCount = pageCount;
    }

    @Deprecated
    public boolean isHasNextPage() {
        return this.getPageNo() < this.getPageCount();
    }

    /**
     * 是否还有下一页
     *
     * @return
     */
    public boolean hasNextPage() {
        return isHasNextPage();
    }

    @Deprecated
    public void setHasNextPage(boolean isHasNextPage) {
    }

    public String getCountAlias() {
        return countAlias;
    }

    public void setCountAlias(String countAlias) {
        this.countAlias = countAlias;
    }

    @Deprecated
    public boolean isHasPrevPage() {
        return this.getPageNo() > 1;
    }

    /**
     * 是否有上一页
     *
     * @return
     */
    public boolean hasPrevPage() {
        return isHasPrevPage();
    }

    @Deprecated
    public void setHasPrevPage(boolean isHasPrevPage) {
    }

    /**
     * 得到上一页，页码数不会变化
     *
     * @return
     */
    public int getPrePage() {
        return (this.pageNo <= 1) ? 1 : this.pageNo - 1;
    }

    /**
     * 上一页，页码数会变化
     *
     * @return
     */
    public int prePage() {
        setPageNo(getPrePage());
        return pageNo;
    }

    @Deprecated
    public void setPrePage(int prePage) {
    }

    /**
     * 得到下一页，页码数不会变化
     *
     * @return
     */
    public int getNextPage() {
        return (this.pageNo >= this.pageCount) ? this.pageCount : this.pageNo + 1;
    }

    /**
     * 下一页，页码数会变化
     *
     * @return
     */
    public int nextPage() {
        setPageNo(getNextPage());
        return pageNo;
    }

    @Deprecated
    public void setNextPage(int nextPage) {

    }

    public int getStartRow() {
        return startRow;
    }

    public void setStartRow(int startRow) {
        if (startRow < 0) {
            startRow = 0;
        }
        this.startRow = startRow;
        reflashRangeAndPageByStartRow();
    }

    protected void reflashRangeAndPageByStartRow() {
        pageNo = startRow / pageSize + 1;
        endRow = startRow + pageSize;
    }

    public int getEndRow() {
        return endRow;
    }

    public void setEndRow(int endRow) {
        if (endRow < pageSize) {
            endRow = pageSize;
        }
        this.endRow = endRow;
        this.startRow = endRow - pageSize;
        reflashRangeAndPageByStartRow();
    }

    public String getOrderByClause() {
        return orderByClause;
    }

    public boolean isPaging() {
        return isPaging;
    }

    public void setPaging(boolean isPaging) {
        this.isPaging = isPaging;
    }

    /**
     * 排序字段，不允许包含关键字 ==final
     *
     * @param orderByClause
     */
    public final void setOrderByClauses(String orderByClause) {
        if (StringUtil.isBlank(orderByClause))
            return;
        orderByClause = orderByClause.toLowerCase();
        for (int i = 0; i < sqlKeys.length; i++) {
            if (orderByClause.indexOf(sqlKeys[i]) >= 0) {
                for (int j = 0; j < filterkeys.length; j++) {
                    if (orderByClause.indexOf(filterkeys[j]) >= 0) {
                        continue;
                    } else {
                        return;
                    }
                }
            }
        }
        this.orderByClause = orderByClause;
    }

    public List<Integer> getCurPageList() {
        List<Integer> list = new ArrayList<Integer>();
        int startPage = getStartPage();
        int endPage = getEndPage();
        if (startPage <= endPage) {
            for (int i = startPage; i <= endPage; i++) {
                list.add(i);
            }
        }
        return list;
    }

    public int getDoPageType() {
        return doPageType;
    }

    public void setDoPageType(int doPageType) {
        this.doPageType = doPageType;
    }

    /**
     *
     * @param pageNo 第几页
     * @param pageSize 每页多少行
     * @return
     */
    public static PageQuery getPageQuery(int pageNo, int pageSize){
        PageQuery pageQuery = new PageQuery();

        if (StringUtil.hasBlank(pageNo)) pageNo = 1;
        if (StringUtil.hasBlank(pageSize)) pageSize = 20;

        pageQuery.setPageNo(pageNo);
        pageQuery.setPageSize(pageSize);
        pageQuery.setPaging(true);

        return pageQuery;
    }

    public static void main(String[] args) {
        PageQuery p = new PageQuery();
        p.setPageNo(2);
        p.setStartRow(21);
        p.setEndRow(155);
        p.setTotalCount(323423);
        System.out.println(p);
    }
}
